﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;

namespace HT.Future.Entities
{
    [Table("T_ProductType")]
    public class ProductType: BaseEntity
    {
        /// <summary>
        /// 分类名称
        /// </summary>
        [Required]
        public string Name { get; set; }
        /// <summary>
        /// 排序码
        /// </summary>
        public int Sort { get; set; }
        /// <summary>
        /// 当前标识状态
        /// </summary>
        [NotMapped]
        public EntitySign Flag { get; set; }
        /// <summary>
        /// 所属分类产品
        /// </summary>
        public virtual ICollection<Product> Products { get; set; }
    }


    /// <summary>
    /// Fluent Api
    /// </summary>
    public class ProductTypeConfiguration : IEntityTypeConfiguration<ProductType>
    {
        public void Configure(EntityTypeBuilder<ProductType> builder)
        {
            builder.HasMany(a => a.Products)
                .WithOne(a => a.ProductType)
                .HasForeignKey(a => a.ProductTypeId)
                .OnDelete(DeleteBehavior.SetNull);      // 当商品类型删除时，将所属的商品其类型属性设置为NULL（无需跟踪对象实体）

        }
    }
}
